class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

function isCycle(head) {
    let p1 = head;
    let p2 = head;
    let isCycle = false;
    let count = 0;
    let isSecondTimeMeat = false;
    let seperateNode = null;
    while (p2 !== null && p2.next !== null) {
        p1 = p1.next;
        p2 = p2.next.next;
        if (p1 === p2) {
            // 第三次相遇在6
            if (isSecondTimeMeat) {
                seperateNode = p1;
                break;
            }
            // 第二次相遇在2
            if (isCycle) {
                isSecondTimeMeat = true;
                p2 = head;
            } else {
                // 第一次相遇在2
                isCycle = true;
            }
        }
        if (!isSecondTimeMeat && isCycle) {
            count++;
        }
    }
    return {isCycle, count, seperateNode};
}

const node5 = new Node(5);
const node3 = new Node(3);
const node7 = new Node(7);
const node2 = new Node(2);
const node6 = new Node(6);
const node8= new Node(8);
const node1 = new Node(1);
node5.next = node3;
node3.next = node7;
node7.next = node2;
node2.next = node6;
node6.next = node8;
node8.next = node1;
node1.next = node2;

console.log(isCycle(node5));